
/*	Table 13: Beta-sorted portfolio overnight and intraday returns	*/

options threads cpucount=8 sortsize=32G bufno=8;
dm log 'clear';

%let mainfolder=D:\Dropbox\Characteristics Factors\RFS_Replication\;

libname mylib "&mainfolder.\Data_Cleaned";

proc datasets library=work nolist kill;
run;
quit;

%let enddate='31DEC2023'd;

*macro to reorder factors so that the output csv is the same order as reported table;
%macro reorder(indata=);

data &indata;
set &indata;
if factor in ('smb',"smb_ff5") then id=1;
if factor in ('hml') then id=2;
if factor in ('rmw') then id=3;
if factor in ('cma') then id=4;
if factor in ('ia') then id=5;
if factor in ('roe') then id=6;
if factor in ('pmu') then id=7;
if factor in ('qmj') then id=8;
if factor in ('umd') then id=9;
if factor in ('mgmt') then id=10;
if factor in ('perf') then id=11;
if factor in ('fin') then id=12;
if factor in ('pead') then id=13;
if factor in ('avg') then id=14;
if factor in ('avg_nosmb') then id=15;
if factor in ('avg_nobf') then id=16;
if factor in ('avg_nobfq') then id=17;
if factor in ('avg_nosmbbf') then id=18;
if factor in ('avg_nosmbbfq') then id=19;
run;

proc sort data=&indata out=&indata(drop=id);
by id;
run;

%mend reorder;

*macro to reorder macro factors so that the output csv is the same order as reported table;
%macro reorder1(indata=);

data &indata;
set &indata;
if factor in ('con') then id=1;
if factor in ('ipg') then id=2;
if factor in ('term') then id=3;
if factor in ('def') then id=4;
if factor in ('ui') then id=5;
if factor in ('dei') then id=6;
if factor in ('vol') then id=7;
if factor in ('mkt') then id=8;
if factor in ('lab') then id=9;
if factor in ('tfp') then id=10;
if factor in ('avg1') then id=11;
if factor in ('avg2') then id=12;
if factor in ('avg3') then id=13;
run;

proc sort data=&indata out=&indata(drop=id);
by id;
run;

%mend reorder1;
****************************************************************************************;

data retd/view=retd;
set mylib.crsp_d(keep=permno date shrcd prc openprc ret);
where 10<=shrcd<=11 and date>='01JUL1992'd;
prc=abs(prc);
openprc=abs(openprc);
intra=log(prc/openprc);
on=log((1+ret)/(prc/openprc));
logret=log(1+ret);
monthend=intnx('month',date,0,'e');
format monthend yymmddn8.;
keep permno date monthend logret on intra;
Run;

proc sort data=retd out=retd1 nodupkey;
by _all_;
where not missing(logret) and not missing(on) and not missing(intra);
run;

proc sort data=retd1 nodupkey;
by permno monthend date;
run;

proc means data=retd1 noprint;
by permno monthend;
var logret on intra;
output out=retd2(drop=_type_ _freq_) sum=ret on intra;
run;

data retd2;
set retd2(rename=(monthend=date));
ret=exp(ret)-1;
on=exp(on)-1;
intra=exp(intra)-1;
keep permno date ret on intra;
run;

proc sort data=retd2 nodupkey;
by permno date;
where not missing(ret) and not missing(on) and not missing(intra);
run;

data ret;
set mylib.crsp;
where date>='01JUN1992'd;
prc=abs(prc);
date=intnx('month',date,0,'e');
format date yymmddn8.;
me=abs(prc)*shrout/1000;*ME in millions;
keep permno date me prc exchcd shrcd siccd;
Run;

proc sort data=ret nodupkey;
by _all_;
where not missing(me);
run;

proc sort data=ret nodupkey;
by permno date descending me;
run;

proc sort data=ret nodupkey;
by permno date;
run;

data lagme(keep=permno date lagme lagprc lagexchcd lagshrcd lagsiccd);
set ret;
date=intnx('month',date,1,'e');
format date yymmddn8.;
rename me=lagme prc=lagprc exchcd=lagexchcd shrcd=lagshrcd siccd=lagsiccd;
run;

proc sort data=lagme nodupkey;
by permno date;
where not missing(permno) and not missing(date) and not missing(lagme);
run;

data all;
merge lagme(in=in1) retd2;
by permno date;
if in1;
run;

data ff3_factors;
set mylib.allfactors(keep=date mktrf);
date=intnx('month',date,0,'e');
format date yymmddn8.;
run;

proc sort data=ff3_factors;
by date;
run;

proc sort data=all nodupkey;
by permno date;
where date<='31DEC2023'd;
run;
***********************************************************************************************************************;


/*	T13 Panel A: characteristics factors beta port	*/
%macro betaport(factor=);

dm 'odsresults; clear';

proc datasets library=work nolist;
save ff3_factors  all  all1 beta table1 avgspread;
run;

%if &factor=umd or &factor=perf or &factor=pead %then %do;

proc sort data=all1(keep=date permno lagme beta1_&factor on intra lagexchcd lagshrcd lagprc freq_&factor) 
out=port1(keep=date permno lagme beta1_&factor on intra lagexchcd rename=(lagexchcd=exchcd)) nodupkey;
by date permno;
where not missing(beta1_&factor) and date<=&enddate and 1<=lagexchcd<=3 and 10<=lagshrcd<=11 and freq_&factor>=&minday;
run;

%end;

%else %do;

proc sort data=all1(keep=date permno lagme beta1_&factor on intra lagexchcd lagshrcd lagprc freq_&factor) 
out=port1(keep=date permno lagme beta1_&factor on intra lagexchcd rename=(lagexchcd=exchcd)) nodupkey;
by date permno;
where not missing(beta1_&factor) and date<=&enddate and 1<=lagexchcd<=3 and 10<=lagshrcd<=11 and freq_&factor>=&minmonth;
run;

%end;

proc univariate data=port1(where=(not missing(beta1_&factor) and 1<=exchcd<=3)) noprint;
by date;
var beta1_&factor;
output out=nyse pctlpre=P_ pctlpts= 10 to 90 by 10;
run;

proc sort data=nyse;
by date;
run;

data port1(drop=P_10 P_20 P_30 P_40 P_50 P_60 P_70 P_80 P_90);
merge port1 nyse;
by date;
if not(beta1_&factor=.) and beta1_&factor<P_10 then cgogroup=0;
if P_10<=beta1_&factor<P_20 then cgogroup=1;
if P_20<=beta1_&factor<P_30 then cgogroup=2;
if P_30<=beta1_&factor<P_40 then cgogroup=3;
if P_40<=beta1_&factor<P_50 then cgogroup=4;
if P_50<=beta1_&factor<P_60 then cgogroup=5;
if P_60<=beta1_&factor<P_70 then cgogroup=6;
if P_70<=beta1_&factor<P_80 then cgogroup=7;
if P_80<=beta1_&factor<P_90 then cgogroup=8;
if beta1_&factor>=P_90 then cgogroup=9;
run;

data port1;
set port1;
portfolio=cgogroup;
if portfolio=. or date=. then delete;
drop cgogroup;
run;

proc sort data=port1;
by date portfolio;
run;

proc means data=port1 noprint;
var on intra;
by date portfolio;
weight lagme;
output out=group_ret(drop=_type_ _Freq_) mean=on intra;
run;

proc sql;
create table group_ret
as select date,portfolio,on,intra 
from group_ret 
group by date
having count(portfolio)=10
order by date,portfolio;
quit;

proc sql;
create table group_ret_6
as select distinct a.date,(a.on-b.on) as on,(a.intra-b.intra) as intra,66 as portfolio
from group_ret as a,group_ret as b
where a.date=b.date and a.portfolio=9 and b.portfolio=0;
quit;

data spread;
set group_ret_6 group_ret;
dif=intra-on;
run; 

proc sort data=spread;
by date;
where portfolio=0 or portfolio=9 or portfolio=66;
run;

data tmp;
set spread;
proxy="&factor";
keep date on intra dif portfolio proxy;
run;

proc append data=tmp base=avgspread force;
run;

proc sort data=spread nodupkey;
by portfolio date;
where not missing(on);
run;

proc sort data=spread out=date(keep=date) nodupkey;
by date;
run;

data firstdate;
set date;
if _n_=1;
run;

proc sort data=date;
by descending date;
run;

data lastdate;
set date;
if _n_=1;
run;

proc sort data=spread nodupkey;
by portfolio date;
run;

ods output parameterestimates=on;
ods listing close;
proc model data=spread;
 by portfolio;
 endo on;
 instruments / intonly;
 parms b0;
 on=b0;
 fit on / gmm kernel=(bart,%eval(&lags+1),0) vardef=n; 
run;
quit;
ods listing;

proc sort data=on(keep=portfolio parameter estimate tvalue) out=on(keep=portfolio estimate tvalue);
by portfolio;
where parameter='b0';
run;

ods output parameterestimates=intra;
ods listing close;
proc model data=spread;
 by portfolio;
 endo intra;
 instruments / intonly;
 parms b0;
 intra=b0;
 fit intra / gmm kernel=(bart,%eval(&lags+1),0) vardef=n; 
run;
quit;
ods listing;

proc sort data=intra(keep=portfolio parameter estimate tvalue) out=intra(keep=portfolio estimate tvalue);
by portfolio;
where parameter='b0';
run;

ods output parameterestimates=dif;
ods listing close;
proc model data=spread;
 by portfolio;
 endo dif;
 instruments / intonly;
 parms b0;
 dif=b0;
 fit dif / gmm kernel=(bart,%eval(&lags+1),0) vardef=n; 
run;
quit;
ods listing;

proc sort data=dif(keep=portfolio parameter estimate tvalue) out=dif(keep=portfolio estimate tvalue);
by portfolio;
where parameter='b0';
run;

data f;
merge on(rename=(estimate=on tvalue=ton)) intra(rename=(estimate=intra tvalue=tintra)) dif(rename=(estimate=dif tvalue=tdif));
by portfolio;
factor="&factor";
on=on*100;
intra=intra*100;
dif=dif*100;
run;
*	add date	;
proc sql;
create table f
as select distinct a.*,b.date as firstdate format=yymmddn8.
from f as a,firstdate as b;
quit;

proc sql;
create table f
as select distinct a.*,b.date as lastdate format=yymmddn8.
from f as a,lastdate as b;
quit;

proc append data=f base=table1 force;
run;

%mend betaport;

%macro suball(avgexclude=);

dm 'odsresults; clear';

proc datasets library=work nolist;
save ff3_factors  all  beta table1 avgspread;
run;

proc sort data=avgspread;
by portfolio date;
where not missing(portfolio);
run;

proc means data=avgspread noprint;
where &avgexclude;
var on intra dif;
by portfolio date;
output out=spread(drop=_type_ _Freq_) mean=on intra dif;
run;

proc sort data=spread nodupkey;
by portfolio date;
where not missing(on);
run;

ods output parameterestimates=on;
ods listing close;
proc model data=spread;
 by portfolio;
 endo on;
 instruments / intonly;
 parms b0;
 on=b0;
 fit on / gmm kernel=(bart,%eval(&lags+1),0) vardef=n; 
run;
quit;
ods listing;

proc sort data=on(keep=portfolio parameter estimate tvalue) out=on(keep=portfolio estimate tvalue);
by portfolio;
where parameter='b0';
run;

ods output parameterestimates=intra;
ods listing close;
proc model data=spread;
 by portfolio;
 endo intra;
 instruments / intonly;
 parms b0;
 intra=b0;
 fit intra / gmm kernel=(bart,%eval(&lags+1),0) vardef=n; 
run;
quit;
ods listing;

proc sort data=intra(keep=portfolio parameter estimate tvalue) out=intra(keep=portfolio estimate tvalue);
by portfolio;
where parameter='b0';
run;

ods output parameterestimates=dif;
ods listing close;
proc model data=spread;
 by portfolio;
 endo dif;
 instruments / intonly;
 parms b0;
 dif=b0;
 fit dif / gmm kernel=(bart,%eval(&lags+1),0) vardef=n; 
run;
quit;
ods listing;

proc sort data=dif(keep=portfolio parameter estimate tvalue) out=dif(keep=portfolio estimate tvalue);
by portfolio;
where parameter='b0';
run;

data f;
merge on(rename=(estimate=on tvalue=ton)) intra(rename=(estimate=intra tvalue=tintra)) dif(rename=(estimate=dif tvalue=tdif));
by portfolio;
on=on*100;
intra=intra*100;
dif=dif*100;
run;

%mend suball;

%macro all(mbeta=,dbeta=);

proc datasets library=work nolist;
save ff3_factors  all ;
run;

data mbeta;
set &mbeta;
date=intnx('month',date,1,'e');
format date yymmddn8.;
drop beta1_umd beta1_pead beta1_perf freq_umd freq_pead freq_perf;
run;

proc sort data=mbeta nodupkey;
by permno date;
run;

data dbeta;
set &dbeta;
date=intnx('month',date,1,'e');
format date yymmddn8.;
run;

proc sort data=dbeta nodupkey;
by permno date;
run;

data beta;
merge mbeta(in=in1) dbeta(in=in2);
by permno date;
run;

data all1;
merge all(in=in1) beta(in=in3);
by permno date;
if in1 and in3;
run;

data table1;
format factor $24.;
format portfolio 8.0;
format on 12.2;
format ton 8.2;
format intra 12.2;
format tintra 8.2;
format dif 12.2;
format tdif 8.2;
format firstdate yymmddn8.;
format lastdate yymmddn8.;
run;

data avgspread;
format proxy $24.;
format portfolio 8.0;
format on 12.6;
format intra 12.6;
format dif 12.6;
format date yymmddn8.;
run;

%betaport(factor=smb_ff5);
%betaport(factor=hml);
%betaport(factor=rmw);
%betaport(factor=cma);
%betaport(factor=ia);
%betaport(factor=roe);
%betaport(factor=pmu);
%betaport(factor=qmj);
%betaport(factor=umd);
%betaport(factor=mgmt);
%betaport(factor=perf);
%betaport(factor=fin);
%betaport(factor=pead);

*	excess return for average portfolio		;
proc datasets library=work nolist;
save ff3_factors  all beta table1 avgspread;
run;
*	with smb		;
proc sort data=avgspread;
by portfolio date;
where not missing(portfolio);
run;

proc means data=avgspread noprint;
var on intra dif;
by portfolio date;
output out=spread(drop=_type_ _Freq_) mean=on intra dif;
run;

proc sort data=spread nodupkey;
by portfolio date;
where not missing(on);
run;

ods output parameterestimates=on;
ods listing close;
proc model data=spread;
 by portfolio;
 endo on;
 instruments / intonly;
 parms b0;
 on=b0;
 fit on / gmm kernel=(bart,%eval(&lags+1),0) vardef=n; 
run;
quit;
ods listing;

proc sort data=on(keep=portfolio parameter estimate tvalue) out=on(keep=portfolio estimate tvalue);
by portfolio;
where parameter='b0';
run;

ods output parameterestimates=intra;
ods listing close;
proc model data=spread;
 by portfolio;
 endo intra;
 instruments / intonly;
 parms b0;
 intra=b0;
 fit intra / gmm kernel=(bart,%eval(&lags+1),0) vardef=n; 
run;
quit;
ods listing;

proc sort data=intra(keep=portfolio parameter estimate tvalue) out=intra(keep=portfolio estimate tvalue);
by portfolio;
where parameter='b0';
run;

ods output parameterestimates=dif;
ods listing close;
proc model data=spread;
 by portfolio;
 endo dif;
 instruments / intonly;
 parms b0;
 dif=b0;
 fit dif / gmm kernel=(bart,%eval(&lags+1),0) vardef=n; 
run;
quit;
ods listing;

proc sort data=dif(keep=portfolio parameter estimate tvalue) out=dif(keep=portfolio estimate tvalue);
by portfolio;
where parameter='b0';
run;

data f;
merge on(rename=(estimate=on tvalue=ton)) intra(rename=(estimate=intra tvalue=tintra)) dif(rename=(estimate=dif tvalue=tdif));
by portfolio;
factor="avg";
on=on*100;
intra=intra*100;
dif=dif*100;
run;

proc append data=f base=table1 force;run;

*	 no smb;
%suball(avgexclude=(not(proxy in ("smb","smb_ff5"))));
data f;
set f;
factor="avg_nosmb";
run;

proc append data=f base=table1 force;run;

*	Organize table based on reported format;
*	T13 Panel A, return;
proc sort data=table1(keep=factor portfolio on ton intra tintra dif tdif) out=table1_1;
by factor;
where not missing(portfolio);
run;

data table1_t13a(drop=portfolio);
merge table1_1(where=(portfolio=0) rename=(on=on0 ton=ton0 intra=intra0 tintra=tintra0 dif=dif0 tdif=tdif0))
table1_1(where=(portfolio=9) rename=(on=on9 ton=ton9 intra=intra9 tintra=tintra9 dif=dif9 tdif=tdif9))
table1_1(where=(portfolio=66) rename=(on=on66 ton=ton66 intra=intra66 tintra=tintra66 dif=dif66 tdif=tdif66));
by factor;
run;

data table1_t13a;
retain factor on0 ton0 intra0 tintra0 dif0 tdif0 on9 ton9 intra9 tintra9 dif9 tdif9 on66 ton66 intra66 tintra66 dif66 tdif66;
set table1_t13a;
run;

%reorder(indata=table1_t13a);

%mend all;

%let lags=5;

*dm log 'clear';

%let minmonth=36;
%let minday=45;

%all(mbeta=mylib.beta1_60,dbeta=mylib.beta2_3);
proc export data=table1_t13a(where=(factor in ("avg","avg_nosmb"))) outfile="&mainfolder.\Result\Table13_PanelA.csv" replace;run;
**********************************************************************************************************************;
**********************************************************************************************************************;
**********************************************************************************************************************;
**********************************************************************************************************************;




/*	T13 Panel B: macro factors beta port	*/
%macro betaport(factor=);

dm 'odsresults; clear';

proc datasets library=work nolist;
save ff3_factors all  all1 beta table1 avgspread;
run;

%if not(&factor=tfp) %then %do;

proc sort data=all1(keep=date permno lagme beta1_&factor._ret on intra lagexchcd lagshrcd lagprc freq_&factor) 
out=port1(keep=date permno lagme beta1_&factor._ret on intra lagexchcd rename=(lagexchcd=exchcd beta1_&factor._ret=beta1_&factor)) nodupkey;
by date permno;
where not missing(beta1_&factor._ret) and date<=&enddate and 1<=lagexchcd<=3 and 10<=lagshrcd<=11 and freq_&factor>=&minmonth;
run;

%end;

%if &factor=tfp %then %do;

proc sort data=all1(keep=date permno lagme beta1_&factor._ret on intra lagexchcd lagshrcd lagprc freq_&factor) 
out=port1(keep=date permno lagme beta1_&factor._ret on intra lagexchcd rename=(lagexchcd=exchcd beta1_&factor._ret=beta1_&factor)) nodupkey;
by date permno;
where not missing(beta1_&factor._ret) and date<=&enddate and 1<=lagexchcd<=3 and 10<=lagshrcd<=11 and freq_&factor>=&minqtr;
run;

%end;

*	need to take negative for def, term, vol	;
%if &factor=def or &factor=term or &factor=vol %then %do;

data port1;
set port1;
beta1_&factor=-beta1_&factor;
run;

proc sort data=port1;
by date;
run;

%end;

proc univariate data=port1(where=(not missing(beta1_&factor) and 1<=exchcd<=3)) noprint;
by date;
var beta1_&factor;
output out=nyse pctlpre=P_ pctlpts= 10 to 90 by 10;
run;

proc sort data=nyse;
by date;
run;

data port1(drop=P_10 P_20 P_30 P_40 P_50 P_60 P_70 P_80 P_90);
merge port1 nyse;
by date;
if not(beta1_&factor=.) and beta1_&factor<P_10 then cgogroup=0;
if P_10<=beta1_&factor<P_20 then cgogroup=1;
if P_20<=beta1_&factor<P_30 then cgogroup=2;
if P_30<=beta1_&factor<P_40 then cgogroup=3;
if P_40<=beta1_&factor<P_50 then cgogroup=4;
if P_50<=beta1_&factor<P_60 then cgogroup=5;
if P_60<=beta1_&factor<P_70 then cgogroup=6;
if P_70<=beta1_&factor<P_80 then cgogroup=7;
if P_80<=beta1_&factor<P_90 then cgogroup=8;
if beta1_&factor>=P_90 then cgogroup=9;
run;

data port1;
set port1;
portfolio=cgogroup;
if portfolio=. or date=. then delete;
drop cgogroup;
run;

proc sort data=port1;
by date portfolio;
run;

proc means data=port1 noprint;
var on intra;
by date portfolio;
weight lagme;
output out=group_ret(drop=_type_ _Freq_) mean=on intra;
run;

proc sql;
create table group_ret
as select date,portfolio,on,intra
from group_ret 
group by date
having count(portfolio)=10
order by date,portfolio;
quit;

proc sql;
create table group_ret_6
as select distinct a.date,(a.on-b.on) as on,(a.intra-b.intra) as intra,66 as portfolio
from group_ret as a,group_ret as b
where a.date=b.date and a.portfolio=9 and b.portfolio=0;
quit;

data spread;
set group_ret_6 group_ret;
dif=intra-on;
run; 

proc sort data=spread;
by date;
where portfolio=0 or portfolio=9 or portfolio=66;
run;

data tmp;
set spread;
proxy="&factor";
keep date on intra dif portfolio proxy;
run;

proc append data=tmp base=avgspread force;
run;

proc sort data=spread nodupkey;
by portfolio date;
where not missing(on);
run;

proc sort data=spread out=date(keep=date) nodupkey;
by date;
run;

data firstdate;
set date;
if _n_=1;
run;

proc sort data=date;
by descending date;
run;

data lastdate;
set date;
if _n_=1;
run;

proc sort data=spread nodupkey;
by portfolio date;
run;

ods output parameterestimates=on;
ods listing close;
proc model data=spread;
 by portfolio;
 endo on;
 instruments / intonly;
 parms b0;
 on=b0;
 fit on / gmm kernel=(bart,%eval(&lags+1),0) vardef=n; 
run;
quit;
ods listing;

proc sort data=on(keep=portfolio parameter estimate tvalue) out=on(keep=portfolio estimate tvalue);
by portfolio;
where parameter='b0';
run;

ods output parameterestimates=intra;
ods listing close;
proc model data=spread;
 by portfolio;
 endo intra;
 instruments / intonly;
 parms b0;
 intra=b0;
 fit intra / gmm kernel=(bart,%eval(&lags+1),0) vardef=n; 
run;
quit;
ods listing;

proc sort data=intra(keep=portfolio parameter estimate tvalue) out=intra(keep=portfolio estimate tvalue);
by portfolio;
where parameter='b0';
run;

ods output parameterestimates=dif;
ods listing close;
proc model data=spread;
 by portfolio;
 endo dif;
 instruments / intonly;
 parms b0;
 dif=b0;
 fit dif / gmm kernel=(bart,%eval(&lags+1),0) vardef=n; 
run;
quit;
ods listing;

proc sort data=dif(keep=portfolio parameter estimate tvalue) out=dif(keep=portfolio estimate tvalue);
by portfolio;
where parameter='b0';
run;

data f;
merge on(rename=(estimate=on tvalue=ton)) intra(rename=(estimate=intra tvalue=tintra)) dif(rename=(estimate=dif tvalue=tdif));
by portfolio;
factor="&factor";
on=on*100;
intra=intra*100;
dif=dif*100;
run;
*	add date	;
proc sql;
create table f
as select distinct a.*,b.date as firstdate format=yymmddn8.
from f as a,firstdate as b;
quit;

proc sql;
create table f
as select distinct a.*,b.date as lastdate format=yymmddn8.
from f as a,lastdate as b;
quit;

proc append data=f base=table1 force;
run;

%mend betaport;

%macro all(inbeta=);

proc datasets library=work nolist;
save ff3_factors  all ;
run;

data beta;
set &inbeta;
date=intnx('month',date,1,'e');
format date yymmddn8.;
run;

proc sort data=beta nodupkey;
by permno date;
run;

data all1;
merge all(in=in1) beta(in=in3);
by permno date;
if in1 and in3;
run;

data table1;
format factor $24.;
format portfolio 8.0;
format on 12.2;
format ton 8.2;
format intra 12.2;
format tintra 8.2;
format dif 12.2;
format tdif 8.2;
format firstdate yymmddn8.;
format lastdate yymmddn8.;
run;

data avgspread;
format proxy $24.;
format portfolio 8.0;
format on 12.6;
format intra 12.6;
format dif 12.6;
format date yymmddn8.;
run;

%betaport(factor=con);
%betaport(factor=ipg);
%betaport(factor=term);
%betaport(factor=def);
%betaport(factor=ui);
%betaport(factor=dei);
%betaport(factor=vol);
%betaport(factor=mkt);
%betaport(factor=lab);
%betaport(factor=tfp);
*	excess return for average portfolio		;
proc datasets library=work nolist;
save ff3_factors  all beta table1 avgspread;
run;
*	ave1=con, tfp, ipg, vol, lab, mkt		;
proc sort data=avgspread;
by portfolio date;
where not missing(portfolio) and not missing(proxy);
run;

proc means data=avgspread noprint;
where not missing(portfolio) and proxy in ('con','tfp','ipg','vol','lab','mkt');
var on intra dif;
by portfolio date;
output out=spread(drop=_type_ _Freq_) mean=on intra dif;
run;

proc sort data=spread nodupkey;
by portfolio date;
where not missing(on);
run;

ods output parameterestimates=on;
ods listing close;
proc model data=spread;
 by portfolio;
 endo on;
 instruments / intonly;
 parms b0;
 on=b0;
 fit on / gmm kernel=(bart,%eval(&lags+1),0) vardef=n; 
run;
quit;
ods listing;

proc sort data=on(keep=portfolio parameter estimate tvalue) out=on(keep=portfolio estimate tvalue);
by portfolio;
where parameter='b0';
run;

ods output parameterestimates=intra;
ods listing close;
proc model data=spread;
 by portfolio;
 endo intra;
 instruments / intonly;
 parms b0;
 intra=b0;
 fit intra / gmm kernel=(bart,%eval(&lags+1),0) vardef=n; 
run;
quit;
ods listing;

proc sort data=intra(keep=portfolio parameter estimate tvalue) out=intra(keep=portfolio estimate tvalue);
by portfolio;
where parameter='b0';
run;

ods output parameterestimates=dif;
ods listing close;
proc model data=spread;
 by portfolio;
 endo dif;
 instruments / intonly;
 parms b0;
 dif=b0;
 fit dif / gmm kernel=(bart,%eval(&lags+1),0) vardef=n; 
run;
quit;
ods listing;

proc sort data=dif(keep=portfolio parameter estimate tvalue) out=dif(keep=portfolio estimate tvalue);
by portfolio;
where parameter='b0';
run;

data f;
merge on(rename=(estimate=on tvalue=ton)) intra(rename=(estimate=intra tvalue=tintra)) dif(rename=(estimate=dif tvalue=tdif));
by portfolio;
factor="avg1";
on=on*100;
intra=intra*100;
dif=dif*100;
run;

proc append data=f base=table1 force;run;

* ave2=ave1+term+def;
proc datasets library=work nolist;
save ff3_factors  all  beta table1 avgspread;
run;

proc means data=avgspread noprint;
where not missing(portfolio) and proxy in ('con','tfp','ipg','vol','lab','mkt','term','def');
var on intra dif;
by portfolio date;
output out=spread(drop=_type_ _Freq_) mean=on intra dif;
run;

proc sort data=spread nodupkey;
by portfolio date;
where not missing(on);
run;

ods output parameterestimates=on;
ods listing close;
proc model data=spread;
 by portfolio;
 endo on;
 instruments / intonly;
 parms b0;
 on=b0;
 fit on / gmm kernel=(bart,%eval(&lags+1),0) vardef=n; 
run;
quit;
ods listing;

proc sort data=on(keep=portfolio parameter estimate tvalue) out=on(keep=portfolio estimate tvalue);
by portfolio;
where parameter='b0';
run;

ods output parameterestimates=intra;
ods listing close;
proc model data=spread;
 by portfolio;
 endo intra;
 instruments / intonly;
 parms b0;
 intra=b0;
 fit intra / gmm kernel=(bart,%eval(&lags+1),0) vardef=n; 
run;
quit;
ods listing;

proc sort data=intra(keep=portfolio parameter estimate tvalue) out=intra(keep=portfolio estimate tvalue);
by portfolio;
where parameter='b0';
run;

ods output parameterestimates=dif;
ods listing close;
proc model data=spread;
 by portfolio;
 endo dif;
 instruments / intonly;
 parms b0;
 dif=b0;
 fit dif / gmm kernel=(bart,%eval(&lags+1),0) vardef=n; 
run;
quit;
ods listing;

proc sort data=dif(keep=portfolio parameter estimate tvalue) out=dif(keep=portfolio estimate tvalue);
by portfolio;
where parameter='b0';
run;

data f;
merge on(rename=(estimate=on tvalue=ton)) intra(rename=(estimate=intra tvalue=tintra)) dif(rename=(estimate=dif tvalue=tdif));
by portfolio;
factor="avg2";
on=on*100;
intra=intra*100;
dif=dif*100;
run;

proc append data=f base=table1 force;run;

* ave3=all 10;
proc datasets library=work nolist;
save ff3_factors  all  beta table1 avgspread;
run;

proc means data=avgspread noprint;
var on intra dif;
by portfolio date;
output out=spread(drop=_type_ _Freq_) mean=on intra dif;
run;

proc sort data=spread nodupkey;
by portfolio date;
where not missing(on);
run;

ods output parameterestimates=on;
ods listing close;
proc model data=spread;
 by portfolio;
 endo on;
 instruments / intonly;
 parms b0;
 on=b0;
 fit on / gmm kernel=(bart,%eval(&lags+1),0) vardef=n; 
run;
quit;
ods listing;

proc sort data=on(keep=portfolio parameter estimate tvalue) out=on(keep=portfolio estimate tvalue);
by portfolio;
where parameter='b0';
run;

ods output parameterestimates=intra;
ods listing close;
proc model data=spread;
 by portfolio;
 endo intra;
 instruments / intonly;
 parms b0;
 intra=b0;
 fit intra / gmm kernel=(bart,%eval(&lags+1),0) vardef=n; 
run;
quit;
ods listing;

proc sort data=intra(keep=portfolio parameter estimate tvalue) out=intra(keep=portfolio estimate tvalue);
by portfolio;
where parameter='b0';
run;

ods output parameterestimates=dif;
ods listing close;
proc model data=spread;
 by portfolio;
 endo dif;
 instruments / intonly;
 parms b0;
 dif=b0;
 fit dif / gmm kernel=(bart,%eval(&lags+1),0) vardef=n; 
run;
quit;
ods listing;

proc sort data=dif(keep=portfolio parameter estimate tvalue) out=dif(keep=portfolio estimate tvalue);
by portfolio;
where parameter='b0';
run;

data f;
merge on(rename=(estimate=on tvalue=ton)) intra(rename=(estimate=intra tvalue=tintra)) dif(rename=(estimate=dif tvalue=tdif));
by portfolio;
factor="avg3";
on=on*100;
intra=intra*100;
dif=dif*100;
run;

proc append data=f base=table1 force;run;

*	T13 Panel B, return;
proc sort data=table1(keep=factor portfolio on ton intra tintra dif tdif) out=table1_1;
by factor;
where not missing(portfolio);
run;

data table1_t13b(drop=portfolio);
merge table1_1(where=(portfolio=0) rename=(on=on0 ton=ton0 intra=intra0 tintra=tintra0 dif=dif0 tdif=tdif0))
table1_1(where=(portfolio=9) rename=(on=on9 ton=ton9 intra=intra9 tintra=tintra9 dif=dif9 tdif=tdif9))
table1_1(where=(portfolio=66) rename=(on=on66 ton=ton66 intra=intra66 tintra=tintra66 dif=dif66 tdif=tdif66));
by factor;
run;

data table1_t13b;
retain factor on0 ton0 intra0 tintra0 dif0 tdif0 on9 ton9 intra9 tintra9 dif9 tdif9 on66 ton66 intra66 tintra66 dif66 tdif66;
set table1_t13b;
run;

%reorder1(indata=table1_t13b);

%mend all;

%let lags=5;

*dm log 'clear';

%let minmonth=36;
%let minqtr=12;
%all(inbeta=mylib.beta_macro_intraday);
proc export data=table1_t13b(where=(factor in ("avg1","avg2","avg3"))) outfile="&mainfolder.\Result\Table13_PanelB.csv" replace;run;
**********************************************************************************************************************;



